package business.database.maauco;


import java.util.HashMap;

import groovy.sql.Sql;
import org.apache.log4j.Logger;


/**
 * Factoria abstracta para los DAO de sqlite
 * @author Administrador
 *
 */
public class AbsMAAUCO1DAOFactory {

	private static AbsMAAUCO1DAOFactory _instance;
	/**
	* Objeto para bloquear y permitir un singleton threadsafe
	*/
	private static Object syncObject_ = new Object();
	private Logger _log;
	/**
	* Este es el objeto que se encarga de acceder a la base de datos SQLite
	*/
   private HashMap<Integer,Sql> sqlco0Pool;
	
	/**
	 * 
	 * @return instancia unica
	 */
	public static AbsMAAUCO1DAOFactory getInstance() {
		if (_instance == null) {
			
						  synchronized(syncObject_) {
			
							if (_instance == null) {
							   _instance = new AbsMAAUCO1DAOFactory();
							}
			
						  }
			
						}
						return _instance;
	}
	
	private AbsMAAUCO1DAOFactory() {
		_log = Logger.getLogger("AbsMAAUCO1DAOFactory");
		sqlco0Pool = new HashMap<Long,Sql>();
	}
	
 /**
   * Cambia la ruta de la base de datos de co0
   * @param path
   */
  public boolean setDB(Integer id, String path) {
	  
	 boolean ok = false
	 
	   def f = new File(path)
	   ok = f.exists()
	   if (ok) {
		   close(id)
		   if (!sqlco0Pool.containsKey(id)) {
			   Sql sqlCO0 = Sql.newInstance("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + path, "",
				   "", "sun.jdbc.odbc.JdbcOdbcDriver"); 
		   	   sqlco0Pool.put(id,sqlCO0);
				  
			  //Creamos un indice sobre corp
			  getCO0DAO(id).createIndexOnCorp();
			  
		   } else sqlco0Pool.get(id).connection.autoCommit = true;
		  }
	   else _log.error "La ruta de CO01: ${path}, es incorrecta"
	   
	   return ok
  }
  
	/**
 * Cierra SQLCO0
 */
  void close(Integer id) {
	 try {
		 if (sqlco0Pool.containsKey(id)) {
			 sqlco0Pool.get(id).close();
			 sqlco0Pool.remove(id);
		 }
	 } catch (Exception e) {
	 	_log.error "Error al cerrar sqlCO0. ${e.getMessage()}"
	 }
 }
  
  /**
  * Cierra todas las conexiones que hayan podido quedar
  */
 public void closeAll() {
	 sqlco0Pool.keySet().each { id ->
		 try {
			  sqlco0Pool.get(id).close();
			 
		 } catch (Exception e) {
			 
		 }
	 }
	 _instance = null;
 }
 
	/**
	* Inicia transaccion en access
	*/
	 public void startTransaction(Integer id) { 
		 sqlco0Pool.get(id).connection.autoCommit = false;
	 }
	 
	 /**
	  * finaliza transaccion en access. Equivalente a hacer un COMMIT
	  */
	 public void endTransaction(Integer id) {
		  sqlco0Pool.get(id).connection.commit();
		  sqlco0Pool.get(id).connection.autoCommit = true;
	 }
	 /**
	  * Hace un rollback sobre la transaccion abierta
	  */
	public void rollback(Integer id) { 
		 sqlco0Pool.get(id).connection.rollback();
		 sqlco0Pool.get(id).connection.autoCommit = true;
	}
	 
	public CO0DAO getCO0DAO(Integer id) {
		return new CO0DAO(sqlco0Pool.get(id));
	}
	
	public MA0DAO getMA0DAO(Integer id) {
		return new MA0DAO(sqlco0Pool.get(id));
	}
	
	public RPsDAO getRPsDAO(Integer id) {
		return new RPsDAO(sqlco0Pool.get(id));
	}
	
	public AU0DAO getAU0DAO(Integer id) {
		return new AU0DAO(sqlco0Pool.get(id));
	}
	
	public CL0DAO getCL0DAO(Integer id) {
		return new CL0DAO(sqlco0Pool.get(id));
	}
	public AUPIODAO getAUPIODAO(Integer id){
		return new AUPIODAO(sqlco0Pool.get(id));
	}
}
